//=============================================================================
//===	Copyright (C) 2001-2007 Food and Agriculture Organization of the
//===	United Nations (FAO-UN), United Nations World Food Programme (WFP)
//===	and United Nations Environment Programme (UNEP)
//===
//===	This program is free software; you can redistribute it and/or modify
//===	it under the terms of the GNU General Public License as published by
//===	the Free Software Foundation; either version 2 of the License, or (at
//===	your option) any later version.
//===
//===	This program is distributed in the hope that it will be useful, but
//===	WITHOUT ANY WARRANTY; without even the implied warranty of
//===	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//===	General Public License for more details.
//===
//===	You should have received a copy of the GNU General Public License
//===	along with this program; if not, write to the Free Software
//===	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
//===
//===	Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
//===	Rome - Italy. email: geonetwork@osgeo.org
//==============================================================================

package org.fao.geonet.guiservices.metadata;

import org.jdom.*;

import jeeves.interfaces.*;
import jeeves.server.*;
import jeeves.server.context.*;

import org.fao.geonet.constants.*;
import org.fao.geonet.kernel.search.*;
import org.fao.geonet.kernel.DataManager;
import org.fao.geonet.GeonetContext;

import java.util.*;

//=============================================================================

/** main.search service. Perform a search
  */

public class GetRandom implements Service
{
	private int     _maxItems;
	private long    _timeBetweenUpdates;

	private Element _response;
	private long    _lastUpdateTime;

	private ServiceConfig _config;

	//--------------------------------------------------------------------------
	//---
	//--- Init
	//---
	//--------------------------------------------------------------------------

	public void init(String appPath, ServiceConfig config) throws Exception
	{
		_config = config;

		String sMaxItems = config.getValue("maxItems", "10");
		_maxItems = Integer.parseInt(sMaxItems);
		String sTimeBetweenUpdates = config.getValue("timeBetweenUpdates", "60");
		_timeBetweenUpdates = Integer.parseInt(sTimeBetweenUpdates) * 1000;

	}

	//--------------------------------------------------------------------------
	//---
	//--- Service
	//---
	//--------------------------------------------------------------------------

	public Element exec(Element params, ServiceContext context) throws Exception
	{
		if (System.currentTimeMillis() > _lastUpdateTime + _timeBetweenUpdates)
		{
			GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);
			DataManager   dataMan   = gc.getDataManager();
			SearchManager searchMan = gc.getSearchmanager();
			MetaSearcher  searcher  = searchMan.newSearcher(SearchManager.LUCENE, Geonet.File.SEARCH_LUCENE);

			// FIXME: featured should be at metadata level, not at group level
			Element searchRequest = new Element("request");
			searchRequest.addContent(new Element("featured").setText("true"));
			searcher.search(context, searchRequest, _config);

			Element presentRequest = new Element("request");
			presentRequest.addContent(new Element("fast").setText("true"));
			presentRequest.addContent(new Element("from").setText("1"));
			presentRequest.addContent(new Element("to").setText(searcher.getSize()+""));
			List results = searcher.present(context, presentRequest, _config).getChildren();

			_response = new Element("response");
			for (int i = 0; i < _maxItems && results.size() > 1; i++)
			{
				Random rnd = new Random();
				int r = rnd.nextInt(results.size() - 1) + 1;  // skip summary

				Element mdInfo = (Element)results.remove(r);
				mdInfo.detach();

				Element info = mdInfo.getChild("info", Edit.NAMESPACE);
				String id = info.getChildText("id");
				Element md = dataMan.getMetadata(context, id, false);

				_response.addContent(md);
			}
			_lastUpdateTime = System.currentTimeMillis();
		}
		return (Element)_response.clone();
	}
}

//=============================================================================

